<?php

/**
 * This is the model class for table "tb_usuarios".
 *
 * The followings are the available columns in table 'tb_usuarios':
 * @property string $login
 * @property string $password
 * @property string $nombre
 * @property string $apellido
 * @property integer $estado
 * @property integer $tb_tipousuarios_idUserType
 *
 * The followings are the available model relations:
 * @property TbActualizacion[] $tbActualizacions
 * @property TbBitacora[] $tbBitacoras
 * @property TbHistorialtrabajo[] $tbHistorialtrabajos
 * @property TbTipousuarios $tbTipousuariosIdUserType
 */
class TbUsuarios extends CActiveRecord
{
	public $estadoActual;
	public $rolActual;
	public $nombreCompleto;
	/**
	 * Returns the static model of the specified AR class.
	 * @return TbUsuarios the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function obtenerEstados(){
		return array('0'=>'Inactivo','1'=>'Activo');
	}
	public function obtenerRoles(){
		return (CHtml::listData(TbTipousuarios::model()->findAll(),'idUserType','tipo'));
		//return array(TbTipousuarios::model()->findAll(),'idUserTye','tipo');
	}
	public function actualizarEstado(){
	}
	
	protected function afterFind(){
		parent::afterFind();
		//actualizarEstado();	
		switch($this->estado){
			case 0: $this->estadoActual = 'Inactivo';
				break;
			case 1: $this->estadoActual = 'Activo';
				break;
			case 2: $this->estadoActual = 'Conectado';
				break;
		}
		
		$tipoUsuario = TbTipousuarios::model()->findByPk($this->tb_tipousuarios_idUserType);
		$this->rolActual = $tipoUsuario->tipo;
		$this->nombreCompleto = $this->nombre." ".$this->apellido;
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tb_usuarios';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('login, password, nombre, apellido, tb_tipousuarios_idUserType', 'required'),
			array('estado, tb_tipousuarios_idUserType', 'numerical', 'integerOnly'=>true),
			array('login, nombre, apellido', 'length', 'max'=>50),
			array('password', 'length', 'max'=>20),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('login, password, nombre, apellido, estado, tb_tipousuarios_idUserType', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'tbActualizacions' => array(self::HAS_MANY, 'TbActualizacion', 'tb_usuarios_login'),
			'tbBitacoras' => array(self::HAS_MANY, 'TbBitacora', 'tb_usuarios_login'),
			'tbHistorialtrabajos' => array(self::HAS_MANY, 'TbHistorialtrabajo', 'tb_usuarios_login'),
			'tbTipousuariosIdUserType' => array(self::BELONGS_TO, 'TbTipousuarios', 'tb_tipousuarios_idUserType'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'login' => 'Login',
			'password' => 'Password',
			'nombre' => 'Nombre',
			'apellido' => 'Apellido',
			'estado' => 'Estado',
			'tb_tipousuarios_idUserType' => 'Rol',
			'estadoActual'=>'Estado',
			'rolActual'=>'Rol',
			'nombreCompleto'=>'Nombre',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('login',$this->login,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('nombre',$this->nombre,true);
		$criteria->compare('apellido',$this->apellido,true);
		$criteria->compare('estado',$this->estado);
		$criteria->compare('tb_tipousuarios_idUserType',$this->tb_tipousuarios_idUserType);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	protected function afterValidate(){
		parent::afterValidate();
		$this->password = ($this->password);
	}
	public function encrypt($value){
		return md5($value);
	}
}